package cn.hchaojie.camp.week2.qa;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;

import org.junit.Test;

public class WordCounter {
	private static final String DATA = "Keep up. The current material is always based on what came before, so once you get behind it is very difficult to catch up. Some students find it is very helpful to quickly look over the notes of the previous lecture right before the current one";
	
	@Test
	public void test() {
		countWords(DATA);
	}
	
	public static void countWords(String s) {
		Map<String, Integer> map = new HashMap<>();
		
		String[] strs = s.split("[ .,]");
		for (String item : strs) {
			String lower = item.toLowerCase();
			
			int count = map.containsKey(lower) ? map.get(lower) : 1;
			map.put(lower, count);
		}

		for (Entry<String, Integer> entry : map.entrySet()) {
			System.out.println(entry.getKey() + "\t" + entry.getValue());
		}
	}
}
